import { createRouter, createWebHistory } from "vue-router"

import { getLogin } from "@/api"
import { getToken } from "@/utils/token"
import MallLayout from "../navigation/mall/MallLayout.vue"
import Recommend from "../navigation/mall/Recommend.vue"
import DogTag from "../navigation/mall/DogTag.vue"
import Insurance from "../navigation/mall/Insurance.vue"
import Traffic from "../navigation/mall/Traffic.vue"
import Home from "@/views/Home.vue"
import Login from "@/views/Login.vue"
import Register from "@/views/Register.vue"
// 导入HomeRecommend组件
import HomeRecommend from "@/components/HomeRecommend.vue"
// 导入HomeSquare组件
import HomeSquare from "@/components/HomeSquare.vue"
// 修复导入路径，AddPet.vue 在 views 目录下而不是 components 目录下
import AddPet from "@/views/AddPet.vue"

const router = createRouter({
    history: createWebHistory(
        import.meta.env.BASE_URL),
    routes: [{
            path: "/",
            redirect: "/home",
        },
        {
            name: "home",
            path: "/home",
            component: Home,
            redirect: "/home/recommend",
            children: [{
                    path: "recommend",
                    component: HomeRecommend,
                },
                {
                    path: "square",
                    component: HomeSquare,
                }
            ]
        },
        {
            name: "home-recommend",
            path: "/home-recommend",
            component: HomeRecommend,
        },
        {
            name: "home-square",
            path: "/home-square",
            component: HomeSquare,
        },
        {
            name: "login",
            path: "/login",
            component: Login,
        },
        {
            name: "MallLayout",
            path: "/mall",
            component: MallLayout,
            redirect: "/mall/recommend",
            children: [
                // 商城子页面：推荐页
                { path: "recommend", name: "recommend", component: Recommend },
                // 商城子页面：犬牌页
                { path: "dog-tag", name: "dogTag", component: DogTag },
                // 商城子页面：保险页
                { path: "insurance", name: "insurance", component: Insurance },
                // 商城子页面：流量页
                { path: "traffic", name: "traffic", component: Traffic },
            ],
        },
        {
            name: "register",
            path: "/register",
            component: Register,
        },
        {
            name: "petAdd",
            path: "/pet/add",
            component: AddPet,
        },
    ],
})

//全局前置路由守卫
/* router.beforeEach((to, from, next) => {
  const token = getToken();
  if (token) {
    if (to.path == "/login" || to.path == "/reg") {
      next({ path: "/" });
    } else {
      next();
    }
  } else {
    next();
  }
}); */

export default router